#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/12/12 15:33
# @Author  : yingxiao zhang
# @File    : choose_stock.py

import tushare as ts, os
import pandas as pd
from collections import Counter

token = ''
ts.set_token(token)
pro = ts.pro_api()

stock_list = pd.read_csv(os.path.join("D:", "stock_list.csv"))
tenyear_grow = dict()
fiveyear_grow = dict()
threeyear_grow = dict()
oneyear_grow = dict()

for i in range(len(stock_list)):
    stock_code = stock_list["ts_code"][i]
    try:
        df_daily = pro.daily(ts_code=stock_code, start_date=20000101, end_date=20210401)
        print("get stock data successsful!", i, stock_code, stock_list['name'][i])
        try:
            if (len(df_daily) >= 250 * 10):
                tenyear_grow[stock_list['name'][i]] = (df_daily['close'][0] - df_daily['close'][2500 - 1]) / \
                                                      df_daily['close'][2500 - 1]
            if (len(df_daily) >= 250 * 5):
                fiveyear_grow[stock_list['name'][i]] = (df_daily['close'][0] - df_daily['close'][250 * 5 - 1]) / \
                                                       df_daily['close'][250 * 5 - 1]
            if (len(df_daily) >= 250 * 3):
                threeyear_grow[stock_list['name'][i]] = (df_daily['close'][0] - df_daily['close'][250 * 3 - 1]) / \
                                                        df_daily['close'][250 * 3 - 1]
            if (len(df_daily) >= 250 * 1):
                oneyear_grow[stock_list['name'][i]] = (df_daily['close'][0] - df_daily['close'][250 * 1 - 1]) / \
                                                      df_daily['close'][250 * 1 - 1]
        except Exception as e:
            pass
    except Exception as e:
        pass
tenyear_grow = Counter(tenyear_grow)
fiveyear_grow = Counter(fiveyear_grow)
threeyear_grow = Counter(threeyear_grow)
oneyear_grow = Counter(oneyear_grow)
print("10年涨幅排行：", tenyear_grow.most_common(50))
print("5年涨幅排行：", fiveyear_grow.most_common(50))
print("3年涨幅排行：", threeyear_grow.most_common(50))
print("1年涨幅排行：", oneyear_grow.most_common(50))